package devmento.gae.resources;


import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import devmento.gae.dao.ArticleDaoJdo;
import devmento.gae.dao.UserDaoJdo;
import devmento.gae.model.Article;
import devmento.gae.model.User;

@Path("/articles")
@Component
public class ArticleResource {
        final Logger logger = LoggerFactory.getLogger(ArticleResource.class);
        
        @Autowired
        private ArticleDaoJdo articleDao;
        
        @Autowired
        private UserDaoJdo userDao;
        
        @GET
        @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        public Article test(){
        	Article article= new Article();
        	article.setContent("test content");
        	article.setTitle("test title");
        	article.setUserId("hong22");
        	List<String> tags = new ArrayList();
        	tags.add("cloud");
        	tags.add("google app engine");
        	article.setTags(tags);
        	return article;
        }
        
        @GET
        @Path("/{id}")
        @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        public Article getArticle(@PathParam("id")long id){
        	return articleDao.find(id);
        }
        
        @DELETE
        @Path("/{id}")
        public void deleteArticle(@PathParam("id")long id){
        	articleDao.delete(id);
        }
        
        @POST
        @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        public void createArticle(Article article){
        	User user = userDao.find(article.getUserId());
        	article.setUserKey(user.getKey());
        	articleDao.create(article);
        }
        
        @PUT
        @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        public void updateArticle(Article article){
        	articleDao.update(article.getContent(), article.getId());
        }
}

